package com.online.teaching.module.common.file.service;

import com.online.teaching.module.common.file.domian.FastDFSFile;
import com.online.teaching.module.common.file.utils.FastDFSClient;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName : FileService  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-26 15:29  //时间
 */
@Service
public class FileService {
    public String upload(MultipartFile file) {
        if(file==null)
        {
            return null;
        }
        try {
            FastDFSFile fastdfsfile = new FastDFSFile(
                    file.getOriginalFilename(),//原来的文件名  1234.jpg
                    file.getBytes(),//文件本身的字节数组
                    StringUtils.getFilenameExtension(file.getOriginalFilename())
            );
            String[] upload = FastDFSClient.upload(fastdfsfile);

            return FastDFSClient.getTrackerUrl()+"/"+upload[0]+"/"+upload[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
